<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <Transition name="fare" mode="out-in">
        <XtxBreadItem :key="topCategory.id">{{topCategory.name}}</XtxBreadItem>
        </Transition>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height:500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in topCategory.children" :key="sub.id">
            <a href="javascript:;">
              <img :src="sub.picture" >
              <p>{{sub.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in subList" :key="item.id">
        <div class="head">
          <h3>- {{item.name}} -</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="goods in item.goods" :key="goods.id" :goods="goods"/>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { findFull } from '@/api/home'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { ref, computed, watch } from 'vue'
import GoodsItem from './components/CategoryGoods.vue'
import { findTopCategory} from '@/api/home'
export default {
  name: 'TopCategory',
  components:{
    GoodsItem
  },
  setup () {
    // 轮播图
    const sliders = ref([])
    findFull().then(data => {
      sliders.value = data.result
    })
    // 获取面包屑加二级分类的数据 在Vuex中
    const store = useStore()
    const route = useRoute()
    //  路由--在Vuex中寻找
    const topCategory = computed(()=>{
      let cate = {}
      const item = store.state.category.list.find(item=>{
      return item.id === route.params.id
    })
    if(item) cate = item
    return cate
    })
    // 获取分类数据
    const subList= ref([])
    const getList = () => {
      findTopCategory(route.params.id).then(data=>{
      subList.value = data.result.children
    })
    }
    // 监听 当路由改变  页面应该初始化
    watch(()=>route.params.id,(newVal)=>{
      // newVal && getList() //要严谨 要一级类目
      if(newVal && `/category/${newVal}`=== route.path) getList()//才可以发请求
       console.log(newVal,'12345')
    },{immediate:true})//执行 watch 函数时立即执行一次回调函数，这样可以避免首次加载页面时需要手动访问 URL 才能触发参数变化从而更新列表数据。
    
    return { sliders, topCategory, subList}  
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
// 关联商品样式
.ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
  // 动画
  // 动画进入时 opacity由0变为1 
  // 动画离开时 opacity由1变为0 
  // 进入前,离开之后
  .fare-enter-from,.fare-leave-to{
    opacity:0;
    transform: translate3d(20px,0,0);
  }
  // 进入后,离开之前
  .fare-enter-to,.fare-leave-from{
    opacity:1;
    transform: none;
  }
  // 进入过程中,离开过程中
  .fare-enter-active,.fare-leave-active{
    transition: all .5s;
  }
 
  // 离开过程中

  // 离开后
  
</style>